package com.tgy.core.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgy.core.mapper.BlogCommentMapper;
import com.tgy.core.service.BlogCommentService;
import com.tgy.page.PageQuery;
import com.tgy.page.TableDataInfo;
import com.tgy.pojo.BlogComment;
import com.tgy.pojo.BlogUser;
import com.tgy.result.LayuiTableResult;
import com.tgy.vo.RecentlyCommentVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 评论信息Service业务层处理
 *
 * @author 自动生成
 * @date 2022-06-20
 */
@Service
public class BlogCommentServiceImpl extends ServiceImpl<BlogCommentMapper, BlogComment> implements BlogCommentService {

    @Resource
    private BlogCommentMapper baseMapper;

    /**
     * 查询评论信息
     *
     * @param id 评论信息主键
     * @return 评论信息
     */
    @Override
    public BlogComment queryById(String id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询评论信息列表
     *
     * @param bo 评论信息
     * @return 评论信息
     */
    @Override
    public TableDataInfo<BlogComment> queryPageList(BlogComment bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogComment> lqw = buildQueryWrapper(bo);
        Page<BlogComment> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public LayuiTableResult queryLayuiPageList(BlogComment bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogComment> lqw = buildQueryWrapper(bo);
        Page<BlogComment> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return new LayuiTableResult(result.getTotal(),result.getRecords());
    }

    /**
     * 查询评论信息列表
     *
     * @param bo 评论信息
     * @return 评论信息
     */
    @Override
    public List<BlogComment> queryList(BlogComment bo) {
        LambdaQueryWrapper<BlogComment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<BlogComment> buildQueryWrapper(BlogComment bo) {
        LambdaQueryWrapper<BlogComment> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getContent()),BlogComment::getContent,bo.getContent());
        lqw.like(StrUtil.isNotBlank(bo.getArticleName()),BlogComment::getArticleName,bo.getArticleName());
        lqw.like(StrUtil.isNotBlank(bo.getUserName()),BlogComment::getUserName,bo.getUserName());
        lqw.eq(Objects.nonNull(bo.getStatus()),BlogComment::getStatus,bo.getStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getArticle()),BlogComment::getArticle,bo.getArticle());
        if (StrUtil.isNotBlank(bo.getReplyId())){
            if (bo.getReplyId().equals("null")){
                lqw.isNull(BlogComment::getReplyId);
            }else if (bo.getReplyId().equals("-1")){
                lqw.ne(BlogComment::getUserId,"1");
            }else {
                lqw.eq(BlogComment::getReplyId,bo.getReplyId());
            }
        }

        lqw.orderByDesc(BlogComment::getCreatedTime);
        return lqw;
    }

    /**
     * 新增评论信息
     *
     * @param bo 评论信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(BlogComment bo) {
        // validEntityBeforeSave(BlogComment);
        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {
            bo.setId(bo.getId());
        }
        return flag;
    }

    /**
     * 修改评论信息
     *
     * @param bo 评论信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(BlogComment bo) {
        // PBizPatient update = BeanUtil.toBean(bo, PBizPatient.class);
        // validEntityBeforeSave(BlogComment);
        return baseMapper.updateById(bo) > 0;
    }

    @Override
    public Boolean deleteById(String id) {
        return baseMapper.deleteById(id) > 0;
    }


    /**
     * 批量删除评论信息
     *
     * @param ids 需要删除的评论信息主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        /*if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }*/
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public int updateStatus(String id, Integer status) {
        return baseMapper.updateStatus(id,status);
    }

    @Override
    public Long queryCommentCount(String articleId) {
        return baseMapper.queryCommentCount(articleId);
    }

    @Override
    public List<RecentlyCommentVo> getBuzzUser() {
        return baseMapper.getBuzzUser();
    }

    @Override
    public List<BlogUser> getUserTopTen() {
        return baseMapper.getUserTopTen();
    }


}
